//
//  AppDelegate.m
//  MGSplitView
//
//  Created by Matt Gemmell on 26/07/2010.
//  Copyright Instinctive Code 2010.
//
#import "DHxlsReaderIOS.h"

#import "AppDelegate.h"
#import "MGTreeViewController.h"
#import "MGChartViewController.h"
#import "MGSplitViewController.h"
#import "MGQuoteViewController.h"

#import "GBall.h"
#import "SourceListItem.h"
#import "PXTableObject.h"
#import "GBTableColumnHeader.h"
#import "GBTableColumnHeader_basicSqlite.h"
#import "GBTableColumnHeader_quote.h"
#import "GBTableColumnHeader_2Row3Row.h"
#import "GBTableColumnHeader_node.h" 
#import "GBTableColumnHeader_position.h"  
#import "GBTableColumnHeader_position_bottom_sumaverage.h"  
#import "GBTableColumnHeader_Indicator.h"  
#import "GBTableColumnHeader_signal.h"  
#import "GBTableColumnHeader_ObjectOtherKey.h"  
#import "GBTableColumnHeader_key.h"
#include "TargetConditionals.h"

#import "XXdevice_Information.h"

@implementation AppDelegate
 

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{

#if TARGET_IPHONE_SIMULATOR
    NSLog (@"判斷裝置：模擬機IPHONE");
    
#elif TARGET_OS_IPHONE
    NSLog (@"判斷裝置：實機IPHONE");
    
#elif TARGET_OS_MAC
    
    NSLog (@"判斷裝置：MAC");
    
#endif
    
    
   
    NSLog(@"裝置 Device_UniqueIdentifier=%@",[XXdevice_Information Device_UniqueIdentifier]);
    NSLog(@"裝置 Device_localizedModel=%@",  [XXdevice_Information Device_localizedModel]);
    
    
    
    NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];    
    
    
    //告訴訊息中心 對某些張貼者有興趣
    {
        NSNotificationCenter *nc =
        [NSNotificationCenter defaultCenter];
        
        [nc addObserver:self
               selector:@selector(received_Quote_Table_finished:)
                   name:NOT_quote_reload
                 object:nil];   
        
        [nc addObserver:self
               selector:@selector(received_Quote_Table_begin:)
                   name:NOT_quote_begin
                 object:nil];             
        
        
        [nc addObserver:self
               selector:@selector(received_His_Data_finished:)
                   name:NOT_his_reload_APP1_Start_WindowController
                 object:nil];  
        
        [nc addObserver:self
               selector:@selector(received_His_Data_begin:)
                   name:NOT_his_begin_APP1_Start_WindowController
                 object:nil];   
        
        
        
        
        /*
         //表格內的文字修改後通知
         [nc addObserver:self
         selector:@selector(textDidEndEditing:)
         name:NSTextDidEndEditingNotification
         object:tabelView_Quote];               
         */
    }
    
    //核心資料模型啟動
    {
        //設定資料來源
        [Data_Model init_datasource];
       
        //設定基本屬性
        [Data_Model init_property];    
        
        //建立必要的目錄 並且將路徑資料存到default.plist
        [Data_Model init_path];
        
        
        datamodel =[[Data_Model alloc] init_data:[prefs objectForKey:@"id_資料源"] 
                                 system_exchange:[prefs objectForKey:Defaults_System_CountryOrExchange] ]; 
        
        
        
         //檢查使用
        NSMutableArray *code_array=[[[NSMutableArray alloc] init] autorelease];        
                 
        SourceListItem *s0=[[datamodel Para_SLT_Paraent_Array] objectAtIndex:0];
        SourceListItem *ss0=[[s0 children] objectAtIndex:1];
        SourceListItem *sss0=[[ss0 children] objectAtIndex:8];
       
        for (Data_Object *cc in  [[sss0 tableobject] data_array_object])  {
           // NSLog(@"s111=%@",[[cc table_data] objectForKey:DB_object_sqlite_basic_code]);  
             [code_array addObject:[[cc table_data] objectForKey:DB_object_sqlite_basic_code]];             
            
        }
        
        //取出一筆歷史資料
        {
            //將需要取出報價的股票代碼轉成陣列
                        
            //[datamodel Quote_Start:code_array];             
        }
    }
        
    
    //window 初始化
    window = [[UIWindow alloc] initWithFrame:[[UIScreen  mainScreen] bounds]] ;
    
    //viewController 初始化
    {
        splitViewController =[[MGSplitViewController alloc] init];
        chartViewController =[[MGChartViewController alloc] init];        
        quoteViewController =[[MGQuoteViewController alloc] init];        
        treeViewController  =[[MGTreeViewController  alloc] init];
                 
             
    }
      
    
    //viewController連接設定
    {
      
        [treeViewController setDatamodel_chart:datamodel];
        [treeViewController setQuoteViewController:quoteViewController];
        
        [quoteViewController   setDelgete_AppDelegate:self]; 
        [quoteViewController   setChartViewController:chartViewController];         
       
        [chartViewController   setSplitController:splitViewController];        
        
        [splitViewController  setDelegate:chartViewController];        
        
        //此設定就是開始啓動viewload  
        [splitViewController  setViewControllers:[NSArray arrayWithObjects:treeViewController,chartViewController,quoteViewController, nil]];    
         
        
    }
    
    
    //VIEW 添加顯示
    if ( [[UIDevice currentDevice].systemVersion floatValue] < 6.0){
        // warning: addSubView doesn't work on iOS6
        [window addSubview:splitViewController.view];
    }
    else{
        // use this mehod on ios6
        [window setRootViewController:splitViewController];
    }
    
    
    
    [window makeKeyAndVisible];
	
    //預設動作
	[treeViewController   performSelector:@selector(selectFirstRow) withObject:nil afterDelay:0];
	[chartViewController  performSelector:@selector(configureView) withObject:nil afterDelay:0];
	
	//預設屬性
    if (NO) { // whether to allow dragging the divider to move the split.
		splitViewController.splitWidth = 5.0; // make it wide enough to actually drag!
		splitViewController.allowsDraggingDivider = YES;
	 }
	
    
    
    return YES;
}


- (void)dealloc
{
    
    [splitViewController release];
    [treeViewController  release];
    [chartViewController release];
    [quoteViewController release];
    
    [window release];
	
    [super dealloc];
}
#pragma mark -
#pragma mark 訊息中心通告


//標記：建立執行緒 7.張貼佈告欄後，通知觀察者
-(void)received_Quote_Table_finished:(NSNotification *)note{
    NSLog(@"received_Quote_Table_finished=%@",note) ;
    
   
    
   
    [treeViewController start_quote_table_refresh];
    //[self start_draw_chart:code];
    
    
     [ActivityIndicator  stopAnimating];      
}
-(void)received_Quote_Table_begin:(NSNotification *)note{
        
    if (ActivityIndicator==nil ) {
        ActivityIndicator=[[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(320,450, 60, 60)];	
        ActivityIndicator.activityIndicatorViewStyle=UIActivityIndicatorViewStyleGray;
        [window addSubview:ActivityIndicator];            
        
    }
    
    [ActivityIndicator  startAnimating];  
    
}
//標記：建立執行緒 7.張貼佈告欄後，通知觀察者
-(void)received_His_Data_finished:(NSNotification *)note{
    NSLog(@"received_His_Data_finished");
    NSLog(@"received_His_Data_finished=%@",[[note userInfo] objectForKey:DB_object_sqlite_basic_code]  );
  
    NSString *code_after_refresh;
    code_after_refresh= [[note userInfo] objectForKey:DB_object_sqlite_basic_code]  ;    
   
    
    //1.取出繪圖物件
    
     
    //繪圖
    if (code_after_refresh) {
               
         [[chartViewController ccchart] drawgraph: [[datamodel object_data_arrary_object_from_code_datamodel: code_after_refresh] get_his_data_serial]];
      
        //測試自動連續下載
          //[treeViewController drawnext_index];    
    }
      
          
    
}
-(void)received_His_Data_begin:(NSNotification *)note{
        
    
}
#pragma mark -
#pragma mark 切換走勢圖
-(void)start_draw_chart:(NSString *)cods{

     [datamodel His_Data_Start:cods];    
    
}
 
@end

